<?php
namespace BP\Pub;
require_once("java/awt/Image.php");
require_once("java/awt/image/BufferedImage.php");
require_once("java/io/BufferedInputStream.php");
require_once("java/io/ByteArrayOutputStream.php");
require_once("java/io/File.php");
require_once("java/io/FileInputStream.php");
require_once("java/io/FileNotFoundException.php");
require_once("java/io/IOException.php");
require_once("java/io/UnsupportedEncodingException.php");
require_once("java/text/DecimalFormat.php");
require_once("java/text/SimpleDateFormat.php");
require_once("java/util/ArrayList.php");
require_once("java/util/Enumeration.php");
require_once("java/util/Hashtable.php");
require_once("java/util/List.php");
require_once("javax/imageio/ImageIO.php");
require_once("BP/DA/Cash.php");
require_once("BP/DA/DBAccess.php");
require_once("BP/DA/DataRow.php");
require_once("BP/DA/DataSet.php");
require_once("BP/DA/DataTable.php");
require_once("BP/DA/DataType.php");
require_once("BP/DA/Log.php");
require_once("BP/En/Attr.php");
require_once("BP/En/Entities.php");
require_once("BP/En/Entity.php");
require_once("BP/En/Map.php");
require_once("BP/Port/Emps.php");
require_once("BP/Sys/FrmAttachmentDB.php");
require_once("BP/Sys/FrmAttachmentDBs.php");
require_once("BP/Sys/PubClass.php");
require_once("BP/Sys/SystemConfig.php");
require_once("BP/Tools/ConvertTools.php");
require_once("BP/Tools/StringHelper.php");
require_once("BP/WF/Glo.php");
class RTFEngine {
	protected $_HisEns;	// Entities
	protected $_EnsDataDtls;	// java.util.ArrayList
	protected $_EnsDataAths;	// Hashtable
	public $CyclostyleFilePath;	// String
	public $TempFilePath;	// String
	public $HisGEEntity;	// Entity
	public $ensStrs;	// String
	public $dtTrack;	// DataTable
	private function __init() { // default class members
		$this->_HisEns = NULL;
		$this->_EnsDataDtls = NULL;
		$this->_EnsDataAths = NULL;
		$this->CyclostyleFilePath = "";
		$this->TempFilePath = "";
		$this->HisGEEntity = NULL;
		$this->ensStrs = "";
		$this->dtTrack = NULL;
	}
	public function getHisEns () 
	{
		if (($this->_HisEns == NULL))
		{
			$this->_HisEns = new Emps();
		}
		return $this->_HisEns;
	}
	public function GetCode ($str) // [String str]
	{
	    if (is_null($str) || $str == '')
		{
			return "";
		}
		$rtn = "";
		$rr = NULL;
		try 
		{
			$rr = $str->getBytes("gb2312");
		}
		catch (UnsupportedEncodingException $e)
		{
			$e->printStackTrace();
		}
		foreach ($rr as $b) 		{
			if (($b > 122))
			{
				$rtn += ("\\'" . $String->format("%x", $b));
			}
			else
			{
				$rtn += $b;
			}
		}
		return $rtn->replace("\n", " \\par ");
	}
	public function getEnsDataDtls () 
	{
		if (($this->_EnsDataDtls == NULL))
		{
			$this->_EnsDataDtls = [];// 
		}
		return $this->_EnsDataDtls;
	}
	public function getEnsDataAths () 
	{
		if (($this->_EnsDataAths == NULL))
			$this->_EnsDataAths = new Hashtable();
		return $this->_EnsDataAths;
	}
	public function AddEn ($en) // [Entity en]
	{
		$this->getHisEns()->AddEntity($en);
	}
	public function AddDtlEns ($dtlEns) // [Entities dtlEns]
	{
		$this->getEnsDataDtls()->add($dtlEns);
	}
	public function GetValueByKeyOfCheckNode ($strs) // [String[] strs]
	{
		foreach ($this->getHisEns() as $en) 		{
			$val = ($en)->GetValStringByKey($strs[2]);
			switch (count($strs) /*from: strs.length*/) {
				case 1:
				case 2:
					throw new \RuntimeException(("step1参数设置错误" . $strs));
				case 3:
					return $val;
				case 4:
					if ($strs[3]->equals("Text"))
					{
						if ($val->equals("0"))
						{
							return "�?";
						}
						else
						{
							return "�?";
						}
					}
					else
						if ($strs[3]->equals("YesNo"))
						{
							if ($val->equals("1"))
							{
								return "[√]";
							}
							else
							{
								return "[×]";
							}
						}
						else
							if ($strs[3]->equals("Year"))
							{
								return $val->substring(0, 4);
							}
							else
								if ($strs[3]->equals("Month"))
								{
									return $val->substring(5, 7);
								}
								else
									if ($strs[3]->equals("Day"))
									{
										return $val->substring(8, 10);
									}
									else
										if ($strs[3]->equals("NYR"))
										else
											if ($strs[3]->equals("RMB"))
											{
												$fnum = new DecimalFormat("##0.00");
												return $fnum->format($val);
											}
											else
												if ($strs[3]->equals("RMBDX"))
												{
													returnDataType->ParseFloatToCash($Float->parseFloat($val));
												}
												else
												{
													throw new \RuntimeException(("step2参数设置错误" . $strs));
												}
				default:
					throw new \RuntimeException(("step3参数设置错误" . $strs));
			}
		}
		throw new \RuntimeException(("step4参数设置错误" . $strs));
	}
	public static function ImageTo16String ($image_path) // [String image_path]
	{
		$fis = NULL;
		$bos = NULL;
		try 
		{
			$imgs = new StringBuilder();
			$fis = new FileInputStream($image_path);
			$bos = new ByteArrayOutputStream();
			$buff = array();
			$len = 0;
			while ((($len = $fis->read($buff)) != -1)) 
			{
				$bos->write($buff, 0, $len);
			}
			$result = $bos->toByteArray();
			for ($i = 0; ($i < count($result) /*from: result.length*/); ++$i) 
			{
				if (((($i % 32)) == 0))
				{
					$imgs->append("\n");
				}
				else
					if (((($i % 8)) == 0))
					{
						$imgs->append(" ");
					}
				$num2 = $result[$i];
				$num3 = ((($num2 & 240)) >> 4);
				$num4 = ($num2 & 15);
				$imgs->append("0123456789abcdef" /* from: "0123456789abcdef".substring(num3, num3 + 1) */);
				$imgs->append("0123456789abcdef" /* from: "0123456789abcdef".substring(num4, num4 + 1) */);
			}
			return $imgs;
		}
		catch (IOException $e)
		{ /* empty */ }
		finally {
		{
			if (($fis != NULL))
			{
				try 
				{
					$fis->close();
				}
				catch (IOException $e)
				{
					$e->printStackTrace();
				}
			}
			if (($bos != NULL))
			{
				try 
				{
					$bos->close();
				}
				catch (IOException $e)
				{
					$e->printStackTrace();
				}
			}
		}
		}
		return "";
	}
	public static function byte2HexStr ($b) // [byte[] b]
	{
		$hs = "";
		$stmp = "";
		for ($n = 0; ($n < count($b) /*from: b.length*/); ++$n) 
		{
			$stmp = ($Integer->toHexString(($b[$n] & 0XFF)));
			if (($stmp->length() == 1))
				$hs = (($hs . "0") . $stmp);
			else
				$hs = ($hs . $stmp);
		}
		return $hs->toUpperCase();
	}
	protected static function uniteBytes ($src0, $src1) // [String src0, String src1]
	{
		$b0 = $Byte->decode(("0x" . $src0))->byteValue();
		$b0 = (($b0 << 4));
		$b1 = $Byte->decode(("0x" . $src1))->byteValue();
		$ret = (($b0 | $b1));
		return $ret;
	}
	public static function bytesToHexString ($src) // [byte[] src]
	{
		$stringBuilder = new StringBuilder("");
		if ((($src == NULL) || (count($src) /*from: src.length*/ <= 0)))
		{
			return NULL;
		}
		for ($i = 0; ($i < count($src) /*from: src.length*/); ++$i) 
		{
			$v = ($src[$i] & 0xFF);
			$hv = $Integer->toHexString($v);
			if (($hv->length() < 2))
			{
				$stringBuilder->append(0);
			}
			$stringBuilder->append($hv);
		}
		return $stringBuilder;
	}
	public static function GetImgHexString ($img, $ext) // [Image img, Image ext]
	{
		return "";
	}
	public function GetValueImgStrs ($key) // [String key]
	{
		$key = $key->replace(" ", "");
		$key = $key->replace("\r\n", "");
		$web_path = $SystemConfig->getPathOfWebApp();
		$path = $key->replace("OID.Img@AppPath", $web_path->substring(0, ($web_path->length() - 1)))->replace("\\\\", "\\");
		$pict = new StringBuilder();
		$image = NULL;
		try 
		{
			$image = $ImageIO->read(new File($path));
		}
		catch (IOException $e)
		{
			$e->printStackTrace();
		}
		$imgHexString = null;
		$imgHexString = RTFEngine::ImageTo16String($path);
		$pict->append("\n");
		$pict->append("{\\pict");
		$pict->append("\\jpegblip");
		$pict->append("\\picscalex100");
		$pict->append("\\picscaley100");
		$pict->append(("\\picwgoal" . ($image->getWidth() * 15)));
		$pict->append(("\\pichgoal" . ($image->getHeight() * 15)));
		$pict->append(($imgHexString . "}"));
		$pict->append("\n");
		return $pict;
	}
	public function GetValueBPPaintStrs ($key) // [String key]
	{
		$key = $key->replace(" ", "");
		$key = $key->replace("\r\n", "");
		$strs = $key->split("[.]", -1);
		$filePath = "";
		try 
		{
			$filePath = DBAccess->RunSQLReturnString((((("SELECT Tag2 From Sys_FrmEleDB WHERE RefPKVal=" . $this->HisGEEntity->getPKVal()) . " AND EleID='") . $strs[2]->trim()) . "'"));
			if (($filePath . NULL))
			{
				return "";
			}
		}
		catch (\Exception $e)
		{
			return "";
		}
		$pict = new StringBuilder();
		$image = NULL;
		try 
		{
			$image = $ImageIO->read(new File($filePath));
		}
		catch (IOException $e)
		{
			$e->printStackTrace();
		}
		$imgHexString = null;
		$filePath = $filePath->toLowerCase();
		$imgHexString = RTFEngine::ImageTo16String($filePath);
		$pict->append("\n");
		$pict->append("{\\pict");
		$pict->append("\\jpegblip");
		$pict->append("\\picscalex100");
		$pict->append("\\picscaley100");
		$pict->append(("\\picwgoal" . ($image->getWidth() * 15)));
		$pict->append(("\\pichgoal" . ($image->getHeight() * 15)));
		$pict->append(($imgHexString . "}"));
		$pict->append("\n");
		return $pict;
	}
	public function GetValueByAtKey ($key) // [String key]
	{
		foreach ($Entities->convertEntities($this->getHisEns()) as $en) 		{
			$enKey = $en;
			if ($enKey->contains("."))
			{
				$enKey = $en->getClass()->getName();
			}
			if (($key->contains(($enKey . "@")) ==  FALSE ))
			{
				continue;
			}
			if (($key->contains(".") ==  FALSE ))
			{
				return $en->GetValStringByKey($key->substring(($key->indexOf('@') + 1)));
			}
			$key = $key->replace(($enKey . "@"), "");
			$strs = $key->split("[.]", -1);
			if ((count($strs) /*from: strs.length*/ == 2))
			{
				if ($strs[1]->trim()->equals("ImgAth"))
				{
					$path1 = (((((SystemConfig::getPathOfDataUser() . "\\ImgAth\\Data\\") . $strs[0]->trim()) . "_") . $en->getPKVal()) . ".png");
					$mypict = new StringBuilder();
					$image = NULL;
					try 
					{
						$image = $ImageIO->read(new File($path1));
					}
					catch (IOException $e)
					{
						$e->printStackTrace();
					}
					$imgHexStringImgAth = RTFEngine::ImageTo16String($path1);
					$mypict->append("\n");
					$mypict->append("{\\pict");
					$mypict->append("\\jpegblip");
					$mypict->append("\\picscalex100");
					$mypict->append("\\picscaley100");
					$mypict->append(("\\picwgoal" . ($image->getWidth() * 15)));
					$mypict->append(("\\pichgoal" . ($image->getHeight() * 15)));
					$mypict->append(($imgHexStringImgAth . "}"));
					$mypict->append("\n");
					return $mypict;
				}
				$val = $en->GetValStringByKey($strs[0]->trim());
				if ($strs[1]->trim()->equals("Text"))
				{
					if ($val->equals("0"))
					{
						return "�?";
					}
					else
					{
						return "�?";
					}
				}
				else
					if ($strs[1]->trim()->equals("Year"))
					{
						return $val->substring(0, 4);
					}
					else
						if ($strs[1]->trim()->equals("Month"))
						{
							return $val->substring(5, 7);
						}
						else
							if ($strs[1]->trim()->equals("Day"))
							{
								return $val->substring(8, 10);
							}
							else
								if ($strs[1]->trim()->equals("NYR"))
								{
									$format = new SimpleDateFormat("yyyy年MM月dd�?");
									return $format->format($DataType->ParseSysDate2DateTime($val));
								}
								else
									if ($strs[1]->trim()->equals("RMB"))
									{
										return new DecimalFormat("##0.00")->format($Float->parseFloat($val));
									}
									else
										if ($strs[1]->trim()->equals("RMBDX"))
										{
											return $DataType->ParseFloatToCash($Float->parseFloat($val));
										}
										else
											if ($strs[1]->trim()->equals("ImgAth"))
											{
												$path1 = (((((SystemConfig::getPathOfDataUser() . "\\ImgAth\\Data\\") . $strs[0]->trim()) . "_") . $this->HisGEEntity->getPKVal()) . ".png");
												$mypict = new StringBuilder();
												$image = NULL;
												try 
												{
													$image = $ImageIO->read(new File($path1));
												}
												catch (IOException $e)
												{
													$e->printStackTrace();
												}
												$imgHexStringImgAth = RTFEngine::ImageTo16String($path1);
												$mypict->append("\n");
												$mypict->append("{\\pict");
												$mypict->append("\\jpegblip");
												$mypict->append("\\picscalex100");
												$mypict->append("\\picscaley100");
												$mypict->append(("\\picwgoal" . ($image->getWidth() * 15)));
												$mypict->append(("\\pichgoal" . ($image->getHeight() * 15)));
												$mypict->append(($imgHexStringImgAth . "}"));
												$mypict->append("\n");
												return $mypict;
											}
											else
												if ($strs[1]->trim()->equals("Siganture"))
												{
													$path = (((SystemConfig::getPathOfDataUser() . "\\Siganture\\") . $val) . ".jpg");
													$pict = new StringBuilder();
													$image = NULL;
													try 
													{
														$image = $ImageIO->read(new File($path));
													}
													catch (IOException $e)
													{
														$e->printStackTrace();
													}
													$imgHexString = RTFEngine::ImageTo16String($path);
													$pict->append("\n");
													$pict->append("{\\pict");
													$pict->append("\\jpegblip");
													$pict->append("\\picscalex100");
													$pict->append("\\picscaley100");
													$pict->append(("\\picwgoal" . ($image->getWidth() * 15)));
													$pict->append(("\\pichgoal" . ($image->getHeight() * 15)));
													$pict->append(($imgHexString . "}"));
													$pict->append("\n");
													return $pict;
												}
												else
												{
													throw new \RuntimeException(("参数设置错误，特殊方式取值错误：" . $key));
												}
			}
		}
		throw new \RuntimeException(("参数设置错误 GetValueByKey �?" . $key));
	}
	public function GetValueByKey ($key) // [String key]
	{
		$key = $key->replace(" ", "");
		$key = $key->replace("\r\n", "");
		if ($key->contains("@"))
		{
			return $this->GetValueByAtKey($key);
		}
		$strs = $key->split("[.]", -1);
		if ((($this->HisGEEntity != NULL) && ($key->contains("ND") ==  FALSE )))
		{
			if ((count($strs) /*from: strs.length*/ == 1))
			{
				return $this->HisGEEntity->GetValStringByKey($key);
			}
			if ($strs[1]->trim()->equals("ImgAth"))
			{
				$path1 = (((((SystemConfig::getPathOfDataUser() . "\\ImgAth\\Data\\") . $strs[0]->trim()) . "_") . $this->HisGEEntity->getPKVal()) . ".png");
				$mypict = new StringBuilder();
				$image = NULL;
				try 
				{
					$image = $ImageIO->read(new File($path1));
				}
				catch (IOException $e)
				{
					$e->printStackTrace();
				}
				$imgHexStringImgAth = RTFEngine::ImageTo16String($path1);
				$mypict->append("\n");
				$mypict->append("{\\pict");
				$mypict->append("\\jpegblip");
				$mypict->append("\\picscalex100");
				$mypict->append("\\picscaley100");
				$mypict->append(("\\picwgoal" . ($image->getWidth() * 15)));
				$mypict->append(("\\pichgoal" . ($image->getHeight() * 15)));
				$mypict->append(($imgHexStringImgAth . "}"));
				$mypict->append("\n");
				return $mypict;
			}
			if ($strs[1]->trim()->equals("BPPaint"))
			{
				$path1 = DBAccess->RunSQLReturnString((((("SELECT  Tag2 FROM Sys_FrmEleDB WHERE REFPKVAL=" . $this->HisGEEntity->getPKVal()) . " AND EleID='") . $strs[0]->trim()) . "'"));
				$mypict = new StringBuilder();
				$image = NULL;
				try 
				{
					$image = $ImageIO->read(new File($path1));
				}
				catch (IOException $e)
				{
					$e->printStackTrace();
				}
				$imgHexStringImgAth = RTFEngine::ImageTo16String($path1);
				$mypict->append("\n");
				$mypict->append("{\\pict");
				$mypict->append("\\jpegblip");
				$mypict->append("\\picscalex100");
				$mypict->append("\\picscaley100");
				$mypict->append(("\\picwgoal" . ($image->getWidth() * 15)));
				$mypict->append(("\\pichgoal" . ($image->getHeight() * 15)));
				$mypict->append(($imgHexStringImgAth . "}"));
				$mypict->append("\n");
				return $mypict;
			}
			if ((count($strs) /*from: strs.length*/ == 2))
			{
				$val = $this->HisGEEntity->GetValStringByKey($strs[0]->trim());
				if ($strs[1]->trim()->equals("Text"))
				{
					if ($val->equals("0"))
					{
						return "�?";
					}
					else
					{
						return "�?";
					}
				}
				else
					if ($strs[1]->trim()->equals("Year"))
					{
						return $val->substring(0, 4);
					}
					else
						if ($strs[1]->trim()->equals("Month"))
						{
							return $val->substring(5, 7);
						}
						else
							if ($strs[1]->trim()->equals("Day"))
							{
								return $val->substring(8, 10);
							}
							else
								if ($strs[1]->trim()->equals("NYR"))
								{
									$dateFormat = new SimpleDateFormat("yyyy年MM月dd�?");
									return $dateFormat->format($DataType->ParseSysDate2DateTime($val));
								}
								else
									if ($strs[1]->trim()->equals("RMB"))
									{
										return new DecimalFormat("##0.00")->format($Float->parseFloat($val));
									}
									else
										if ($strs[1]->trim()->equals("RMBDX"))
										{
											return $DataType->ParseFloatToCash($Float->parseFloat($val));
										}
										else
											if ($strs[1]->trim()->equals("Siganture"))
											{
												$path = (((SystemConfig::getPathOfDataUser() . "\\Siganture\\") . $val) . ".jpg");
												$pict = new StringBuilder();
												$image = NULL;
												try 
												{
													$image = $ImageIO->read(new File($path));
												}
												catch (IOException $e)
												{
													$e->printStackTrace();
												}
												$imgHexStringImgAth = RTFEngine::ImageTo16String($path);
												$pict->append("\n");
												$pict->append("{\\pict");
												$pict->append("\\jpegblip");
												$pict->append("\\picscalex100");
												$pict->append("\\picscaley100");
												$pict->append(("\\picwgoal" . ($image->getWidth() * 15)));
												$pict->append(("\\pichgoal" . ($image->getHeight() * 15)));
												$pict->append(($imgHexStringImgAth . "}"));
												$pict->append("\n");
												return $pict;
											}
											else
												if ($strs[1]->trim()->equals("BoolenText"))
												{
													if ($val->equals("0"))
														return "�?";
													else
														return "�?";
												}
												else
													if ($strs[1]->trim()->equals("Boolen"))
													{
														if ($val->equals("1"))
															return "[√]";
														else
															return "[×]";
													}
													else
														if ($strs[1]->trim()->equals("YesNo"))
														{
															if ($val->equals("1"))
															{
																return "[√]";
															}
															else
															{
																return "[×]";
															}
														}
														else
															if ($strs[1]->trim()->equals("Yes"))
															{
																if ($val->equals("0"))
																{
																	return "[×]";
																}
																else
																{
																	return "[√]";
																}
															}
															else
																if ($strs[1]->trim()->equals("No"))
																{
																	if ($val->equals("0"))
																	{
																		return "[√]";
																	}
																	else
																	{
																		return "[×]";
																	}
																}
																else
																{
																	throw new \RuntimeException(("参数设置错误，特殊方式取值错误：" . $key));
																}
			}
			else
			{
				throw new \RuntimeException(("参数设置错误，特殊方式取值错误：" . $key));
			}
		}
		throw new \RuntimeException(("参数设置错误 GetValueByKey �?" . $key));
	}
	public function MakeDoc_String ($cfile) // [String cfile]
	{
		$file = $PubClass->GenerTempFileName("doc");
			/* match: String_String_String_b */
		$this->MakeDoc_String_String_String_b($cfile, $SystemConfig->getPathOfTemp(), $file,  TRUE );
	}
	public function MakeDoc_String_String_String_b ($cfile, $path, $file, $isOpen) // [String cfile, String path, String file, boolean isOpen]
	{
		$cfile = $cfile->replace(".rtf.rtf", ".rtf");
		if ((new File($path)->exists() ==  FALSE ))
			new File($path)->mkdirs();
		$str = new StringBuilder($Cash->GetBillStr($cfile,  FALSE )->substring(0));
		if (($this->getHisEns()->size() == 0))
		{
			if (($this->HisGEEntity == NULL))
			{
				throw new \RuntimeException("@您没有为报表设置数据�?...");
			}
		}
		$this->ensStrs = "";
		if (($this->getHisEns()->size() != 0))
		{
			foreach ($Entities->convertEntities($this->getHisEns()) as $en) 			{
				$this->ensStrs += $en;
			}
		}
		else
		{
			$this->ensStrs = $this->HisGEEntity;
		}
		$error = "";
		$paras = NULL;
		if (($this->HisGEEntity != NULL))
		{
			$paras = $Cash->GetBillParas($cfile, $this->ensStrs, $this->HisGEEntity);
		}
		else
		{
			$paras = $Cash->GetBillParas($cfile, $this->ensStrs, $this->getHisEns());
		}
		$this->TempFilePath = ($path . $file);
		try 
		{
			$key = "";
			$ss = "";
			foreach ($paras as $para) 			{
				if ((($para == NULL) || $para->equals("")))
				{
					continue;
				}
				try 
				{
					if ($para->contains("ImgAth"))
						$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
					else
						if ($para->contains("Siganture"))
							$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
						else
							if ($para->contains("Img@AppPath"))
								$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueImgStrs($para)));
							else
								if ($para->contains(".BPPaint"))
									$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueBPPaintStrs($para)));
								else
									if ($para->contains(".RMB"))
										$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
									else
										if ($para->contains(".RMBDX"))
											$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
										else
											if ($para->contains(".Boolen"))
												$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
											else
												if ($para->contains(".BoolenText"))
													$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
												else
													if ($para->contains(".NYR"))
														$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
													else
														if ($para->contains(".Year"))
															$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
														else
															if ($para->contains(".Month"))
																$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
															else
																if ($para->contains(".Day"))
																	$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
																else
																	if (($para->contains(".Yes") ==  TRUE ))
																		$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetCode($this->GetValueByKey($para))));
																	else
																		if (($para->contains("-EnumYes") ==  TRUE ))
																			$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetCode($this->GetValueByKey($para))));
																		else
																			if (($para->contains(".") ==  TRUE ))
																				continue;
																			else
																			{
																				$str = new StringBuilder($str->replace((("<" . $para) . ">"), $this->GetValueByKey($para)));
																			}
				}
				catch (\RuntimeException $ex)
				{
					$error += ((("替换主表标记取参数[" . $para) . "]出现错误：有以下情况导致此错�?;1你用Text取�?�时间，此属性不是外键�??2,类无此属性�??3,该字段是明细表字段但是丢失了明细表标�?.<br>更详细的信息�?<br>") . $ex->getMessage());
					$Log->DebugWriteError(("MakeDoc" . $error));
					if ($SystemConfig->getIsDebug())
					{
						throw new \RuntimeException($error);
					}
				}
			}
			$shortName = "";
			$al = $this->getEnsDataDtls();
			foreach ($al as $dtls) 			{
				$dtl = $dtls->getNewEntity();
				$dtlEnName = $dtl;
				$shortName = $dtlEnName->substring(($dtlEnName->lastIndexOf(".") + 1));
				if (($str->indexOf($shortName) == -1))
				{
					continue;
				}
				$pos_rowKey = $str->indexOf($shortName);
				$end_rowKey = $str->lastIndexOf($shortName);
				$row_start = -1$row_end = -1;
				if (($pos_rowKey != -1))
				{
					$row_start = $str->substring(0, $pos_rowKey)->lastIndexOf("\\row");
					$row_end = $str->substring($end_rowKey)->indexOf("\\row");
				}
				if ((($row_start != -1) && ($row_end != -1)))
				{
					$row = $str->substring($row_start, (($end_rowKey) + $row_end));
					$str = new StringBuilder($str->replace($row, ""));
					$map = $dtls->getNewEntity()->getEnMap();
					$i = $dtls->size();
					while (($i > 0)) 
					{
						--$i;
						$tempVar = $row;
						$rowData = (( (($tempVar instanceof String)) ? $tempVar : NULL ));
						$dtl = $dtls->getItem($i);
						$rowIdx = ($i + 1);
						$rowData = $rowData->replace("<IDX>", $String->valueOf($rowIdx));
						foreach ($map->getAttrs() as $attr) 						{
							if (!$attr->getUIVisible())
							{
								continue;
							}
							switch ($attr->getMyDataType()) {
								case $DataType->AppDouble:
								case $DataType->AppFloat:
								case $DataType->AppRate:
									$rowData = $rowData->replace((((("<" . $shortName) . ".") . $attr->getKey()) . ">"), $dtl->GetValStringByKey($attr->getKey()));
									break;
								case $DataType->AppMoney:
									$rowData = $rowData->replace((((("<" . $shortName) . ".") . $attr->getKey()) . ">"), $dtl->GetValDecimalByKey($attr->getKey(), 2));
									break;
								case $DataType->AppInt:
									if (($attr->getMyDataType() == $DataType->AppBoolean))
									{
										$rowData = $rowData->replace((((("<" . $shortName) . ".") . $attr->getKey()) . ">"), $dtl->GetValStrByKey($attr->getKey()));
										$v = $dtl->GetValIntByKey($attr->getKey());
										if (($v == 1))
										{
											$rowData = $rowData->replace((((("<" . $shortName) . ".") . $attr->getKey()) . "Text>"), "�?");
										}
										else
										{
											$rowData = $rowData->replace((((("<" . $shortName) . ".") . $attr->getKey()) . "Text>"), "�?");
										}
									}
									else
									{
										if ($attr->getIsEnum())
										{
											$rowData = $rowData->replace((((("<" . $shortName) . ".") . $attr->getKey()) . "Text>"), $dtl->GetValRefTextByKey($attr->getKey()));
										}
										else
										{
											$rowData = $rowData->replace((((("<" . $shortName) . ".") . $attr->getKey()) . ">"), $dtl->GetValStrByKey($attr->getKey()));
										}
									}
									break;
								default:
									$rowData = $rowData->replace((((("<" . $shortName) . ".") . $attr->getKey()) . ">"), $dtl->GetValStrByKey($attr->getKey()));
									break;
							}
						}
						$str = $str->insert($row_start, $rowData);
					}
				}
			}
			$al = $this->getEnsDataDtls();
			foreach ($al as $dtls) 			{
				$dtl = $dtls->getNewEntity();
				$dtlEnName = $dtl;
				$shortName = $dtlEnName->substring(($dtlEnName->lastIndexOf(".") + 1));
				$map = $dtl->getEnMap();
				foreach ($map->getAttrs() as $attr) 				{
					switch ($attr->getMyDataType()) {
						case $DataType->AppDouble:
						case $DataType->AppFloat:
						case $DataType->AppMoney:
						case $DataType->AppRate:
							$key = (((("<" . $shortName) . ".") . $attr->getKey()) . ".SUM>");
							if (($str->indexOf($key) != -1))
							{
								$str = new StringBuilder($str->replace($key, (new Float($dtls->GetSumFloatByKey($attr->getKey())))));
							}
							$key = (((("<" . $shortName) . ".") . $attr->getKey()) . ".SUM.RMB>");
							if (($str->indexOf($key) != -1))
							{
								$value = new DecimalFormat("##0.00")->format(new Float($dtls->GetSumFloatByKey($attr->getKey())));
								$str = new StringBuilder($str->replace($key, $value));
							}
							$key = (((("<" . $shortName) . ".") . $attr->getKey()) . ".SUM.RMBDX>");
							if (($str->indexOf($key) != -1))
							{
								$str = new StringBuilder($str->replace($key, $this->GetCode($DataType->ParseFloatToCash($dtls->GetSumFloatByKey($attr->getKey())))));
							}
							break;
						case $DataType->AppInt:
							$key = (((("<" . $shortName) . ".") . $attr->getKey()) . ".SUM>");
							if (($str->indexOf($key) != -1))
							{
								$str = new StringBuilder($str->replace($key, (new Integer($dtls->GetSumIntByKey($attr->getKey())))));
							}
							break;
						default:
							break;
					}
				}
			}
			if (((($this->dtTrack != NULL) && ($str->contains("<WorkCheckBegin>") ==  FALSE )) && ($str->contains("<WorkCheckEnd>") ==  FALSE )))
			{
				foreach ($this->dtTrack->Rows as $row) 				{
					$acType = $Integer->parseInt($row->getValue("ActionType"));
					if (($acType != 22))
						continue;
					$str = new StringBuilder($str->replace((("<WorkCheck.Msg." . $row->getValue("NDFrom")) . ">"), $this->GetValueCheckWorkByKey($row, "Msg")));
					$str = new StringBuilder($str->replace((("<WorkCheck.Rec." . $row->getValue("NDFrom")) . ">"), $this->GetValueCheckWorkByKey($row, "EmpFromT")));
					$str = new StringBuilder($str->replace((("<WorkCheck.RDT." . $row->getValue("NDFrom")) . ">"), $this->GetValueCheckWorkByKey($row, "RDT")));
				}
			}
			foreach ($this->getEnsDataAths()->keySet() as $athObjEnsName) 			{
				$athName = ("Ath." . $athObjEnsName);
				$athFilesName = "";
				if (($str->indexOf($athName) == -1))
					continue;
				$athDbs = $this->getEnsDataAths()->get($athObjEnsName);
				if (($athDbs == NULL))
					continue;
				foreach ($athDbs->ToJavaList() as $athDb) 				{
					if (($athFilesName->length() > 0))
						$athFilesName += " �? ";
					$athFilesName += $athDb->getFileName();
				}
				$str = new StringBuilder($str->replace((("<" . $athName) . ">"), $this->GetCode($athFilesName)));
			}
			$str = new StringBuilder($str->replace("<", ""));
			$str = new StringBuilder($str->replace(">", ""));
			try 
			{
				$ConvertTools->streamWriteConvertGBK($str, $this->TempFilePath);
			}
			catch (Exception $e)
			{
				$e->printStackTrace();
			}
		}
		catch (\RuntimeException $ex)
		{
			$msg = "";
			if ($SystemConfig->getIsDebug())
			{
				try 
				{
					$this->CyclostyleFilePath = (($SystemConfig->getPathOfDataUser() . "/CyclostyleFile/") . $cfile);
					$str = new StringBuilder($Cash->GetBillStr($cfile,  FALSE ));
					$s = RepBill::RepairBill($this->CyclostyleFilePath);
					$msg = ("@已经成功的执行修复线  RepairLineV2，您重新发�?�一次或者，�?后重新在发�?�一次，是否可以解决此问题�?�@" . $s);
				}
				catch (\RuntimeException $ex1)
				{
					$msg = ("执行修复线失�?.  RepairLineV2 " . $ex1->getMessage());
				}
			}
			throw new \RuntimeException(((((("生成文档失败：单据名称[" . $this->CyclostyleFilePath) . "] 异常信息�?") . $ex->getMessage()) . " @自动修复单据信息�?") . $msg));
		}
		if ($isOpen)
		{
			$PubClass->Print((($Glo->getCCFlowAppPath() . "Temp/") . $file));
		}
	}
	protected function GetValueCheckWorkByKey ($row, $key) // [DataRow row, String key]
	{
		$key = $key->replace(" ", "");
		$key = $key->replace("\r\n", "");
		switch ($key) {
			case "RDT":
				return $row->getValue("RDT");
			case "RDT-NYR":
				$rdt = $row->getValue("RDT");
				return $rdt;
			case "Rec":
				return $row->getValue("EmpFrom");
			case "RecName":
				return $row->getValue("EmpFromT");
			case "Msg":
			case "Note":
				return $row->getValue("Msg");
			default:
				return $row->getValue($key);
		}
	}
	abstract function MakeDocByDataSet ($templeteFile, $saveToPath, $saveToFileName, $mainDT, $dtlsDS); // [String templeteFile, String saveToPath, String saveToFileName, DataTable mainDT, DataSet dtlsDS]
	public static function constructor__ () 
	{
		$me = new self();
		$me->__init();
		$me->_EnsDataDtls = NULL;
		$me->_HisEns = NULL;
		return $me;
	}
}
?>
